Conditions | 11 |
Paths | 14 |
Total Lines | 56 |
Code Lines | 32 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like jquery.flot.spline.js ➔ drawSpline often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /** |
||
138 | function drawSpline(plot, ctx, series) { |
||
139 | // Not interested if spline is not requested |
||
140 | if (series.splines.show !== true) { |
||
141 | return; |
||
142 | } |
||
143 | |||
144 | var cp = [], |
||
145 | // array of control points |
||
146 | tension = series.splines.tension || 0.5, |
||
147 | idx, x, y, points = series.datapoints.points, |
||
148 | ps = series.datapoints.pointsize, |
||
149 | plotOffset = plot.getPlotOffset(), |
||
150 | len = points.length, |
||
151 | pts = []; |
||
152 | |||
153 | line = []; |
||
154 | |||
155 | // Cannot display a linespline/areaspline if there are less than 3 points |
||
156 | if (len / ps < 4) { |
||
157 | $.extend(series.lines, series.splines); |
||
158 | return; |
||
159 | } |
||
160 | |||
161 | for (idx = 0; idx < len; idx += ps) { |
||
162 | x = points[idx]; |
||
163 | y = points[idx + 1]; |
||
164 | if (x == null || x < series.xaxis.min || x > series.xaxis.max || y < series.yaxis.min || y > series.yaxis.max) { |
||
165 | continue; |
||
166 | } |
||
167 | |||
168 | pts.push(series.xaxis.p2c(x) + plotOffset.left, series.yaxis.p2c(y) + plotOffset.top); |
||
169 | } |
||
170 | |||
171 | len = pts.length; |
||
172 | |||
173 | // Draw an open curve, not connected at the ends |
||
174 | for (idx = 0; idx < len - 2; idx += 2) { |
||
175 | cp = cp.concat(getControlPoints.apply(this, pts.slice(idx, idx + 6).concat([tension]))); |
||
176 | } |
||
177 | |||
178 | ctx.save(); |
||
179 | ctx.strokeStyle = series.color; |
||
180 | ctx.lineWidth = series.splines.lineWidth; |
||
181 | |||
182 | queue(ctx, 'quadratic', pts.slice(0, 4), cp.slice(0, 2)); |
||
183 | |||
184 | for (idx = 2; idx < len - 3; idx += 2) { |
||
185 | queue(ctx, 'bezier', pts.slice(idx, idx + 4), cp.slice(2 * idx - 2, 2 * idx + 2)); |
||
186 | } |
||
187 | |||
188 | queue(ctx, 'quadratic', pts.slice(len - 2, len), [cp[2 * len - 10], cp[2 * len - 9], pts[len - 4], pts[len - 3]]); |
||
189 | |||
190 | drawLine(line, ctx, plot.height() + 10, series.splines.fill, series.color); |
||
191 | |||
192 | ctx.restore(); |
||
193 | } |
||
194 | |||
213 |